import {ElLoading, ElMessage} from "element-plus";
import AppRequest from "../../ark/request/app-request";
import {MockMode} from "../../ark/request/app-request-type";

// 环境变量
const envBaseURL = import.meta.env.VITE_API_CONTEXT_PATH
    ? import.meta.env.VITE_API_CONTEXT_PATH
    : "/";

const envMockMode = [MockMode.MOCKER, MockMode.MIX].includes(import.meta.env.VITE_MOCK_MODE)
    ? import.meta.env.VITE_MOCK_MODE
    : MockMode.SERVER;

const envMockDefault = import.meta.env.VITE_MOCK_DEFAULT === "true";

const envMockTarget = import.meta.env.VITE_MOCK_TARGET
    ? import.meta.env.VITE_MOCK_TARGET
    : "";

// 请求对象
const request = new AppRequest({
      // ###### Axios配置 #######
      timeout: 60 * 1000 * 10,
      baseURL: envBaseURL,

      // ###### 自定义配置 #######
      showLoading: true,
      showLoadingFunction() {
        ElLoading.service({
          lock: true,
          text: '加载中...',
        });
      },
      hideLoadingFunction(isSuccess: boolean) {
        ElLoading.service({}).close();
      },

      addRequestHeaders(headers) {
        // 此处常用于添加权限验证token请求头
        // const tokenInfo = useGeneralStore()?.tokenInfo;
        // if (tokenInfo.value) {
        //     headers[tokenInfo.key] = `Bearer ${tokenInfo.value}`;
        // }
      },

      isServerSuccess(serverResponse) {
        return serverResponse.code == "200";
      },
      isTokenInvalid(serverResponse) {
        return ["A0301", "A0302", "A0303", "A0304"].includes(serverResponse.code);
      },
      handleTokenInvalid() {
        // 此处常用于退出登录并跳转登录页
        // useGeneralStore().logout();
        // Router.replace("/");
      },

      showSuccessMsg: false,
      showSuccessMsgFunction(msg) {
        ElMessage.success(msg);
      },

      showFailureMsg: true,
      showFailureMsgFunction(msg) {
        ElMessage.error(msg);
      },
    },
    {
      mode: envMockMode,
      defaultMock: envMockDefault,
      defaultMockerTarget: envMockTarget,
      /*urlConfig: [
        {
          url: '/back_end_get/params',
          mock: true
        }
      ]*/
    });

export default request;